package com.richman.view;

import javax.swing.JPanel;
import javax.swing.JTextField;

import com.richman.utils.Common;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;

public class SettingPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9999999L;

	/**
	 * Create the panel.
	 */
	
	private JComboBox comboBox;
	
	private String second = "500";
	
	private JTextField sideMinTextField;
	private JTextField sumMinTextField;
	private JTextField hgAmountTextField;
	private JTextField ps38AmountTextField;
	private JTextField sideMaxTextField;
	private JTextField sumMaxTextField;
	
	private double sideMinodd = 0.0;
	private double sumMinodd = 0.0;
	
	private double sideMaxodd = 0.0;
	private double sumMaxodd = 0.0;
	
	
	private JTextField danshiMinTimeField;
	private JTextField danshiMaxTimeField;
	private JTextField gqHalfMinTimeField;
	private JTextField gqHalfMaxTimeField;
	private JTextField gqWholeMinTimeField;
	private JTextField gqWholeMaxTimeField;
	
	
	private int danshiMinTime = 0;
	private int danshiMaxTime = 0;
	private int gqHalfMinTime = 0;
	private int gqHalfMaxTime = 0;
	private int gqWholeMinTime = 0;
	private int gqWholeMaxTime = 0;
	
	String[] seconds = {"300毫秒", "600毫秒", "1000毫秒", "1500毫秒",  "2000毫秒", "3000毫秒"};
	
	ForbidLeagueFrame forbidLeagueFrame = new ForbidLeagueFrame();
	NegLeagueFrame negLeagueFrame = new NegLeagueFrame();
	
	
	public SettingPane() {
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("抓取间隔时间");
		lblNewLabel.setBounds(10, 29, 97, 26);
		add(lblNewLabel);
		
		
		
		
		comboBox = new JComboBox(seconds);
		
		comboBox.setSelectedIndex(5);
		
		comboBox.setBounds(110, 32, 85, 26);
		add(comboBox);
		
		
		
		
		
		
		
		
		JLabel label_3 = new JLabel("过滤单式开场低于");
		label_3.setBounds(10, 83, 116, 26);
		add(label_3);
		
		danshiMinTimeField = new JTextField();
		danshiMinTimeField.setColumns(10);
		danshiMinTimeField.setBounds(129, 83, 66, 21);
		add(danshiMinTimeField);
		
		JLabel label_4 = new JLabel("过滤单式开场高于");
		label_4.setBounds(10, 129, 116, 26);
		add(label_4);
		
		danshiMaxTimeField = new JTextField();
		danshiMaxTimeField.setColumns(10);
		danshiMaxTimeField.setBounds(129, 129, 66, 21);
		add(danshiMaxTimeField);
		
		JLabel label_5 = new JLabel("过滤滚球半场低于");
		label_5.setBounds(233, 83, 116, 26);
		add(label_5);
		
		gqHalfMinTimeField = new JTextField();
		gqHalfMinTimeField.setColumns(10);
		gqHalfMinTimeField.setBounds(347, 83, 66, 21);
		add(gqHalfMinTimeField);
		
		JLabel label_6 = new JLabel("过滤滚球半场高于");
		label_6.setBounds(233, 119, 116, 26);
		add(label_6);
		
		gqHalfMaxTimeField = new JTextField();
		gqHalfMaxTimeField.setColumns(10);
		gqHalfMaxTimeField.setBounds(347, 124, 66, 21);
		add(gqHalfMaxTimeField);
		
		JLabel label_7 = new JLabel("过滤滚球全场低于");
		label_7.setBounds(442, 83, 116, 26);
		add(label_7);
		
		JLabel label_8 = new JLabel("过滤滚球全场高于");
		label_8.setBounds(442, 119, 116, 26);
		add(label_8);
		
		gqWholeMinTimeField = new JTextField();
		gqWholeMinTimeField.setColumns(10);
		gqWholeMinTimeField.setBounds(562, 83, 66, 21);
		add(gqWholeMinTimeField);
		
		gqWholeMaxTimeField = new JTextField();
		gqWholeMaxTimeField.setColumns(10);
		gqWholeMaxTimeField.setBounds(562, 119, 66, 21);
		add(gqWholeMaxTimeField);
		
		
		
		
		
		
		
		JLabel sideLb = new JLabel("过滤赔率低于");
		sideLb.setBounds(10, 171, 97, 26);
		add(sideLb);
		
		sideMinTextField = new JTextField();
		sideMinTextField.setBounds(110, 171, 66, 21);
		add(sideMinTextField);
		sideMinTextField.setColumns(10);
		
		JLabel sumLb = new JLabel("过滤对比赔率低于");
		sumLb.setBounds(227, 171, 122, 26);
		add(sumLb);
		
		sumMinTextField = new JTextField();
		sumMinTextField.setColumns(10);
		sumMinTextField.setBounds(359, 171, 66, 21);
		add(sumMinTextField);
		

		

		
		JLabel label = new JLabel("过滤赔率高于");
		label.setBounds(10, 220, 97, 26);
		add(label);
		
		sideMaxTextField = new JTextField();
		sideMaxTextField.setColumns(10);
		sideMaxTextField.setBounds(110, 220, 66, 21);
		add(sideMaxTextField);
		
		JLabel label_1 = new JLabel("过滤对比赔率高于");
		label_1.setBounds(227, 220, 122, 26);
		add(label_1);
		
		
		
		JButton forbidLeagueBtn = new JButton("禁投联赛");
		forbidLeagueBtn.setBounds(10, 280, 100, 20);
		add(forbidLeagueBtn);
		
		forbidLeagueFrame.setVisible(false);
		
		forbidLeagueBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//显示一个jfame
				forbidLeagueFrame.setVisible(true);
				}
		}
		);
		
		
		JButton negLeagueBtn = new JButton("反投联赛");
		negLeagueBtn.setBounds(150, 280, 100, 20);
		add(negLeagueBtn);
		
		negLeagueFrame.setVisible(false);
		
		negLeagueBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//显示一个jfame
				negLeagueFrame.setVisible(true);
				}
		}
		);
		
		

		
		
		sumMaxTextField = new JTextField();
		sumMaxTextField.setColumns(10);
		sumMaxTextField.setBounds(359, 220, 66, 21);
		add(sumMaxTextField);
		
		
		comboBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {				
	            second = comboBox.getSelectedItem().toString().replace("毫秒", "");	            
	            System.out.println(second);
			}
	    });
		
		
		JButton saveButton = new JButton("保存设置");
		saveButton.setBounds(400, 400, 145, 48);
		add(saveButton);
		
		
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = sideMinTextField.getText();
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤赔率低于输入不正确");
					return;
				}
				
				str = sumMinTextField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤对比赔率低于输入不正确");
					return;
				}
				
				str = sideMaxTextField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤赔率高于输入不正确");
					return;
				}
				
				
				str = sumMaxTextField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤对比赔率高于输入不正确");
					return;
				}
				
				
				
				
				

				
				
				
				str = danshiMinTimeField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤时间输入错误，请检查");
					return;
				}
				
				
				str = danshiMaxTimeField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤时间输入错误，请检查");
					return;
				}
				
				
				str = gqHalfMinTimeField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤时间输入错误，请检查");
					return;
				}
				
				str = gqHalfMaxTimeField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤时间输入错误，请检查");
					return;
				}
				
				
				str = gqWholeMinTimeField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤时间输入错误，请检查");
					return;
				}
				
				str = gqWholeMaxTimeField.getText();
				
				if(!Common.isNum(str)) {
					JOptionPane.showMessageDialog(null,"过滤时间输入错误，请检查");
					return;
				}
				
			
				
				
				if(savebetparamstofile()) {
					
					JOptionPane.showMessageDialog(null,"保存设置成功");

					sideMinodd = Double.parseDouble(sideMinTextField.getText());
					sumMinodd = Double.parseDouble(sumMinTextField.getText());
					sideMaxodd = Double.parseDouble(sideMaxTextField.getText());
					sumMaxodd = Double.parseDouble(sumMaxTextField.getText());

					
					danshiMinTime = Integer.parseInt(danshiMinTimeField.getText());
					danshiMaxTime = Integer.parseInt(danshiMaxTimeField.getText());
					gqHalfMinTime = Integer.parseInt(gqHalfMinTimeField.getText());
					gqHalfMaxTime = Integer.parseInt(gqHalfMaxTimeField.getText());
					gqWholeMinTime = Integer.parseInt(gqWholeMinTimeField.getText());
					gqWholeMaxTime = Integer.parseInt(gqWholeMaxTimeField.getText());
					
					
				}else {
					JOptionPane.showMessageDialog(null,"保存设置失败");
					
					
				}
				
			}
		});
		
		
		recoverbetparamsfromfile();

	}
	

	public int getDanshiMinTime() {
		return danshiMinTime;
	}


	


	public int getDanshiMaxTime() {
		return danshiMaxTime;
	}




	public int getGqHalfMinTime() {
		return gqHalfMinTime;
	}




	public int getGqHalfMaxTime() {
		return gqHalfMaxTime;
	}




	public int getGqWholeMinTime() {
		return gqWholeMinTime;
	}




	public int getGqWholeMaxTime() {
		return gqWholeMaxTime;
	}





	public int getUpdateSeconds() {
		
		if(Common.isNum(second)) {
			return Integer.parseInt(second);
		}else {
			return 3000;
		}
		
	}
	
	public double getsideMin() {
		return sideMinodd;
	}
	
	public double getsumMin() {
		return sumMinodd;
	}
	
	
	public double getsideMax() {
		return sideMaxodd;
	}
	
	public double getsumMax() {
		return sumMaxodd;
	}
	
	
	public void recoverbetparamsfromfile(){
		try{
			
			File file = new File("data" + "/sesttingparams.txt");
			
			if(!file.exists()){
				return;
			}

			BufferedReader freader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			
			
			
			
			
			String str = "";
			
			int linenum = 0;

			while ((str = freader.readLine()) != null) {
				

				if(linenum == 0) {
					
					if(Common.isNum(str)) {
						
						int index = 0;
						
						for(int i= 0; i < seconds.length; i++) {
							String secStr = seconds[i];
							
							if(secStr.equals(str)) {
								index = i;
								break;
							}
							
						}
						
						comboBox.setSelectedIndex(index);
						
						second = str;
						
					}
					
					
					
					
					
					
					
					
				}else if(linenum == 1) {
					if(Common.isNum(str)) {
						sideMinodd = Double.parseDouble(str);
						sideMinTextField.setText(str);
					}
				}else if(linenum == 2) {
					if(Common.isNum(str)) {
						sumMinodd = Double.parseDouble(str);
						sumMinTextField.setText(str);
					}
				}else if(linenum ==3) {
					if(Common.isNum(str)) {
						sideMaxodd = Double.parseDouble(str);
						sideMaxTextField.setText(str);
					}
				}else if(linenum == 4) {
					if(Common.isNum(str)) {
						sumMaxodd = Double.parseDouble(str);
						sumMaxTextField.setText(str);
					}
				}else if(linenum == 5) {
					if(Common.isNum(str)) {
						danshiMinTime = Integer.parseInt(str);
						danshiMinTimeField.setText(str);
					}
				}else if(linenum == 6) {
					if(Common.isNum(str)) {
						danshiMaxTime = Integer.parseInt(str);
						danshiMaxTimeField.setText(str);
					}
				}else if(linenum == 7) {
					if(Common.isNum(str)) {
						gqHalfMinTime = Integer.parseInt(str);
						gqHalfMinTimeField.setText(str);
					}
				}else if(linenum == 8) {
					if(Common.isNum(str)) {
						gqHalfMaxTime = Integer.parseInt(str);
						gqHalfMaxTimeField.setText(str);
					}
				}else if(linenum ==9) {
					if(Common.isNum(str)) {
						gqWholeMinTime = Integer.parseInt(str);
						gqWholeMinTimeField.setText(str);
					}
				}else if(linenum == 10) {
					if(Common.isNum(str)) {
						gqWholeMaxTime = Integer.parseInt(str);
						gqWholeMaxTimeField.setText(str);
					}
				}
				
				
				linenum++;

			}
			
			
			
			
			if(null != freader){
				freader.close();
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	public boolean savebetparamstofile(){
		try{
			
			File filefolder = new File("data/");
			
			if(!filefolder.exists()){
				filefolder.mkdir();
			}
			
			File file = new File("data/"+ "sesttingparams.txt");

			if(!file.exists()){	
				file.createNewFile();
			}
			
			BufferedWriter fwlocal = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));

			
			
			
			
			String str = comboBox.getSelectedItem().toString().replace("毫秒", "");	
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();		
			
			str = sideMinTextField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			
			str = sumMinTextField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			str = sideMaxTextField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			str = sumMaxTextField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			

			
			
			
			str = danshiMinTimeField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			str = danshiMaxTimeField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();

			str = gqHalfMinTimeField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			str = gqHalfMaxTimeField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			
			str = gqWholeMinTimeField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			str = gqWholeMaxTimeField.getText();
			
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			fwlocal.close();

			return true;
			
			
			
		}catch(Exception e){
			e.printStackTrace();
			
		}
		
		return false;
	}


	public void generateForbidLeagueItem() {
		// TODO Auto-generated method stub
		forbidLeagueFrame.generateItem();
		negLeagueFrame.generateItem();
	}
	
	public Vector<String> getForbidLeagues(){
		return forbidLeagueFrame.getForbidLeagues();
	}
	
	public Vector<String> getNegLeagues(){
		return negLeagueFrame.getNegLeagues();
	}

}
